<?php

App::uses('Crud', 'Model');
App::uses('User', 'Model');
App::uses('Schedule', 'Model');
App::uses('Room', 'Model');

class TransportRequest extends Crud
{
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

    public function getEnabled() {
        $filters = array(
            'fields' => array(
                'TransportRequest.id as value',
                'u.full_name as user',
                's.description as event',
                'TransportRequest.service_date as sDate',
                'TransportRequest.quantity as cant'
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'inner',
                    'conditions' => array(
                        'TransportRequest.user_id = u.id',
                        'u.status' => User::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'schedules',
                    'alias' => 's',
                    'type' => 'inner',
                    'conditions' => array(
                        'TransportRequest.schedule_id = s.id',
                        's.status' => Schedule::STATUS_ENABLED
                    )
                )
            ),
            'conditions' => array(
                'TransportRequest.status' => self::STATUS_ENABLED
            ),
            'order' => array(
                'sDate ASC'
            )
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function getEnabledById($requestId) {
        $filters = array(
            'fields' => array(
                'u.full_name as user',
                'TransportRequest.service_date as sDate',
                'TransportRequest.quantity as cant',
                'TransportRequest.creation_date as cDate',
                's.end as eDate',
                's.description as motive',
                'r.name as place',
                'TransportRequest.type as type'
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'inner',
                    'conditions' => array(
                        'TransportRequest.user_id = u.id',
                        'u.status' => User::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'schedules',
                    'alias' => 's',
                    'type' => 'inner',
                    'conditions' => array(
                        'TransportRequest.schedule_id = s.id',
                        's.status' => Schedule::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'rooms',
                    'alias' => 'r',
                    'type' => 'inner',
                    'conditions' => array(
                        's.room_id = r.id',
                        'r.status' => Room::STATUS_ENABLED
                    )
                )
            ),
            'conditions' => array(
                'TransportRequest.id' => $requestId,
                'TransportRequest.status' => self::STATUS_ENABLED
            ),
            'order' => array(
                'sDate ASC'
            )
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
}